package org.theSeed.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.theSeed.base.exp.BuzExp;
import org.theSeed.base.pojo.constant.DateConstant;
import org.theSeed.base.pojo.sysEnum.SysCode;
import org.theSeed.fileConfig.config.SeedFileHolder;
import org.theSeed.fileConfig.utils.SeedFileUtil;
import org.theSeed.fileContract.pojo.dto.*;
import org.theSeed.fileContract.pojo.vo.*;
import org.theSeed.pojo.ro.*;
import org.theSeed.service.FileService;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private SeedFileHolder seedFileHolder;

    @SneakyThrows
    @Override
    public SeedUploadFileProxyVo uploadFile(MultipartFile file){
        SeedUploadFileDto param = new SeedUploadFileDto();
        param.setFileName(file.getOriginalFilename());
        param.setIs(file.getInputStream());
        param.setRelativePath(getRealatePath());
        return SeedFileUtil.uploadFile(param);
    }

    @SneakyThrows
    @Override
    public SeedUploadFileProxyVo updateFile(UpdateFileRo updateFileRo) {
        SeedUpdateFileProxyDto param = new SeedUpdateFileProxyDto();
        param.setIs(updateFileRo.getFile().getInputStream());
        param.setResourceUrl(updateFileRo.getResourceUrl());
        return SeedFileUtil.updateFile(param);
    }

    @Override
    public SeedUploadFileProxyVo deleteFile(ResourceBo resourceBo) {
        SeedDeleteFileProxyDto param = new SeedDeleteFileProxyDto();
        param.setResourceUrl(resourceBo.getResourceUrl());
        return SeedFileUtil.deleteFile(param);
    }

    @Override
    public SeedUploadChipFileInitProxyVo initUploadChipFile(UploadChipFileInitRo uploadChipFileInitRo) {
        SeedUploadChipFileInitDto param = new SeedUploadChipFileInitDto();
        param.setFileName(uploadChipFileInitRo.getFileName());
        param.setFileSize(uploadChipFileInitRo.getFileSize());
        param.setRelativePath(getRealatePath());
        return SeedFileUtil.initUploadChipFile(param);
    }

    @SneakyThrows
    @Override
    public SeedUploadFileProxyVo uploadChipFile(UploadChipFileRo uploadChipFileRo) {
        SeedUploadChipFileProxyDto param = new SeedUploadChipFileProxyDto();
        param.setChipIndex(uploadChipFileRo.getChipIndex());
        param.setIs(uploadChipFileRo.getFile().getInputStream());
        param.setResourceId(uploadChipFileRo.getResourceId());
        param.setEncodeExtInfo(uploadChipFileRo.getEncodeExtInfo());
        return SeedFileUtil.uploadChipFile(param);
    }

    @Override
    public SeedUploadFileProxyVo mergeChipFile(MergeChipFileRo mergeChipFileRo) {
        MergeChipFileProxyDto param = new MergeChipFileProxyDto();
        param.setResourceId(mergeChipFileRo.getResourceId());
        param.setEncodeExtInfo(mergeChipFileRo.getEncodeExtInfo());
        return SeedFileUtil.mergeChipFile(param);
    }

    @Override
    public SeedCheckChipFileProxyVo checkChipFile(CheckChipFileRo checkChipFileRo) {
        CheckChipFileProxyDto param = new CheckChipFileProxyDto();
        param.setResourceId(checkChipFileRo.getResourceId());
        param.setEncodeExtInfo(checkChipFileRo.getEncodeExtInfo());
        return SeedFileUtil.checkChipFile(param);
    }

    @Override
    public CancleChipFileProxyVo cancleChipFile(CancleChipFileRo cancleChipFileRo) {
        CancleChipFileProxyDto param = new CancleChipFileProxyDto();
        param.setResourceId(cancleChipFileRo.getResourceId());
        param.setEncodeExtInfo(cancleChipFileRo.getEncodeExtInfo());
        return SeedFileUtil.cancleChipFile(param);
    }

    @SneakyThrows
    @Override
    public void getFile(String resourceUrl, HttpServletResponse resp) {
        SeedGetFileProxyDto param = new SeedGetFileProxyDto();
        param.setResourceUrl(resourceUrl);
        try (InputStream file = SeedFileUtil.getFile(param)){
            IoUtil.copy(file,resp.getOutputStream());
        }catch (Exception exp){
            throw new BuzExp(SysCode.FAIL,"文件流读取失败");
        }
    }

    /**
     * 获取相对路径
     * @return
     */
    public String getRealatePath(){
        return DateUtil.format(new Date(), DateConstant.DATE);
    }
}
